function export_ambient_cubes _stream _coordsys =
(
	print _stream
	local mapCh = 0
	for isel = 1 to selection.count do
	(
		local m_node = selection[isel]
		
		if not (isKindOf m_node GeometryClass) and not (isKindOf m_node Box) do
		(
			format "cannot export %, not geometry(%)\n" m_node.name (classOf m_node)
			continue
		)
		
		local m = snapshotasmesh m_node
		if (m == undefined) or not(meshop.getMapSupport m mapCh) do
		(
			format "cannot export %, snapshotasmesh failed\n" m_node.name
			continue
		)
		
-- 		local pos = [0.0, 0.0, 0.0]
-- 		local numVert = getNumVerts m
-- 		for ivert = 1 to numVert do
-- 		(
-- 			pos = pos + (meshop.getVert m ivert node:m_node)
-- 		)
-- 		
-- 		pos = pos / numVert
		local pos = m_node.center
		
		local rm = RayMeshGridIntersect()
		rm.Initialize 2
		rm.addNode m_node
		rm.buildGrid()
		
		(
		--local numVert = getNumVerts m
		--local numFace = getNumFaces m
		
		--local numClrVert = meshop.getNumMapVerts m 0
		--local numClrFace = meshop.getNumMapFaces m 0
		
		--format "mesh[%] vert & face: % & %\n" pos numVert numFace
		--format "mesh color map vert & face: % & %\n" numClrVert numClrFace
		)
		
		local basisDir = #(\
			[1.0, 0.0, 0.0], [-1.0, 0.0, 0.0], \
			[0.0, 1.0, 0.0], [ 0.0,-1.0, 0.0], \
			[0.0, 0.0, 1.0], [ 0.0, 0.0,-1.0] )
			
		local basisClr = #(\
			[0.0, 0.0, 0.0], [ 0.0, 0.0, 0.0], \
			[0.0, 0.0, 0.0], [ 0.0, 0.0, 0.0], \
			[0.0, 0.0, 0.0], [ 0.0, 0.0, 0.0] )
			
		for ibasis = 1 to basisDir.count do
		(
			local dir = basisDir[ibasis]
			local hitsCount = rm.intersectRay pos dir true
			
			if hitsCount > 0 do
			(
				hitIndex = rm.getClosestHit()
				hitFace = rm.getHitFace hitIndex
				hitBary = rm.getHitBary hitIndex
				
				--format "hitinfo: dir % index % face % bary %\n" dir hitIndex hitFace hitBary
				
				-- get the vertex colors of the hitting face
				local clrFace = meshop.getMapFace m mapCh hitFace
				local clrVert = #()
				append clrVert (meshop.getMapVert m mapCh clrFace[1])
				append clrVert (meshop.getMapVert m mapCh clrFace[2])
				append clrVert (meshop.getMapVert m mapCh clrFace[3])
				
				basisClr[ibasis] = clrVert[1] * hitBary[1] + clrVert[2] * hitBary[2] + clrVert[3] * hitBary[3]
			)
		)
		
		-- map the index to the correct coordinate system
		local idx_3dsmax = #(1, 2, 3, 4, 5, 6) -- x y z
		local idx_opengl = #(1, 2, 5, 6, 4, 3)	-- x z -y
		local idx_d3d = #(1, 2, 5, 6, 3, 4) -- x z y
		
		-- matrix (transposed) for axis conversion
		local mtx_3dsmax = #([1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0])
		local mtx_opengl = #([1.0, 0.0, 0.0], [0.0, 0.0, 1.0], [0.0,-1.0, 0.0])
		local mtx_d3d = #([1.0, 0.0, 0.0], [0.0, 0.0, 1.0], [0.0, 1.0, 0.0])
		
		local idx = idx_3dsmax
		local mtx = mtx_3dsmax
		
		if  _coordsys == "3dsmax" do (idx = idx_3dsmax; mtx = mtx_3dsmax)
		if  _coordsys == "opengl" do (idx = idx_opengl; mtx = mtx_opengl)
		if  _coordsys == "d3d" do (idx = idx_d3d; mtx = mtx_d3d)
		
		-- write result
		format "%,%,%;" (dot pos mtx[1]) (dot pos mtx[2]) (dot pos mtx[3]) to: _stream
		
		for ibasis = 1 to basisDir.count do
		(
			local clr = basisClr[ idx[ibasis] ]
			format "%,%,%;" clr[1] clr[2] clr[3] to: _stream
		)
		
		format "\n" to: _stream
		
		-- clean up
		rm = undefined
		m = undefined
	)
)